package com.wang.commons.utils;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class BeanCopyUtil {
    //
    public static <T> T copyProperties(Object obj, Class<T> clazz) {
        T t = null;
        try {
            t = clazz.getConstructor().newInstance();
            BeanUtils.copyProperties(obj, t);
        } catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return t;
    }
    public static <T> List<T> copyList(List< ? extends Object> objs, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        objs.forEach(obj->{
            list.add(copyProperties(obj,clazz));
        });
        return list;
    }
}
